package com.learn.jms.test;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 《Spring源码深度解析》第13章 Spring消息
 * 接收端实现
 * ActiveMQ下载：http://activemq.apache.org/activemq-5015009-release
 * Windows系统下双击 /bin/win64/activemq.bat 启动消息服务器
 */
public class Receiver {
    public static void main(String[] args) throws Exception{
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        Connection connection = connectionFactory.createConnection();
        connection.start();

        final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("my-queue");
        MessageConsumer consumer = session.createConsumer(destination);

        int i = 0;
        while(i<3){
            i++;
            TextMessage message = (TextMessage) consumer.receive();
            session.commit();
            //TODO something...
            System.out.println("收到消息：" + message.getText());
        }

        session.close();
        connection.close();
    }
}
/* Output:
 INFO | Successfully connected to tcp://localhost:61616
 收到消息：大家好这是个测试
 收到消息：大家好这是个测试
 收到消息：大家好这是个测试
*///~
